<%#
 Copyright 2013-2020 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
<%_
const tsKeyId = generateTestEntityId(primaryKeyType);
_%>
/* tslint:disable max-line-length */
import { shallowMount, createLocalVue, Wrapper } from '@vue/test-utils';
import sinon, { SinonStubbedInstance } from 'sinon';
import Router from 'vue-router';

<%_
    let dateFnsIncluded = false;
    for (idx in fields) {
        const fieldName = fields[ idx ].fieldName;
        const fieldType = fields[idx].fieldType;
        if ([ 'Instant', 'ZonedDateTime' ].includes(fieldType) && !dateFnsIncluded) {
            dateFnsIncluded = true;
_%>
import format from 'date-fns/format';
import parseISO from 'date-fns/parseISO';
import { DATE_TIME_LONG_FORMAT } from '@/shared/date/filters';
<%_ } } _%>

import * as config from '@/shared/config/config';
import <%= entityAngularName %>UpdateComponent from '@/entities/<%= entityFolderName %>/<%= entityFileName %>-update.vue';
import <%= entityAngularName %>Class from '@/entities/<%= entityFolderName %>/<%= entityFileName %>-update.component';
import <%= entityAngularName %>Service from '@/entities/<%= entityFolderName %>/<%= entityFileName %>.service';

<%_
    const importEntitiesSeen = [entityAngularName];
    for (idx in relationships) {
      if (!relationships[idx].otherEntityIsEmbedded) {
        const otherEntityAngularName = relationships[idx].otherEntityAngularName;
        const otherEntityFileName = relationships[idx].otherEntityFileName;
        const otherEntityFolderName = relationships[idx].otherEntityFileName;
        const otherEntityClientRootFolder = relationships[idx].otherEntityClientRootFolder;
        const otherEntityModelName = relationships[idx].otherEntityModelName;
        if (!importEntitiesSeen.includes(otherEntityAngularName)) {
_%>
  <% if (otherEntityAngularName === 'User' && authenticationType !== 'oauth2') { %>
import UserService from '@/admin/user-management/user-management.service';
  <% } else if (otherEntityAngularName !== 'User') { %>
import <%= otherEntityAngularName %>Service from '@/entities/<%= otherEntityClientRootFolder %><%= otherEntityFolderName %>/<%= otherEntityFileName %>.service';
  <% } } %>
<%_ importEntitiesSeen.push(otherEntityAngularName); } } _%>

const localVue = createLocalVue();

config.initVueApp(localVue);
<%_ if (enableTranslation) { _%>
const i18n = config.initI18N(localVue);
<%_ } _%>
const store = config.initVueXStore(localVue);
const router = new Router();
localVue.use(Router);
localVue.component('font-awesome-icon', {});
localVue.component('b-input-group', {});
localVue.component('b-input-group-prepend', {});
localVue.component('b-form-datepicker', {});
localVue.component('b-form-input', {});

describe('Component Tests', () => {

  describe('<%= entityAngularName %> Management Update Component', () => {
    let wrapper: Wrapper<<%= entityAngularName %>Class>;
    let comp: <%= entityAngularName %>Class;
    let <%= entityInstance %>ServiceStub: SinonStubbedInstance<<%= entityAngularName %>Service>;

    beforeEach(() => {
      <%= entityInstance %>ServiceStub = sinon.createStubInstance<<%= entityAngularName %>Service>(<%= entityAngularName %>Service);

      wrapper = shallowMount<<%= entityAngularName %>Class>(<%= entityAngularName %>UpdateComponent, {
        store,
        <%_ if (enableTranslation) { _%>
        i18n,
        <%_ } _%>
        localVue,
        router,
        provide: {
          <%= entityInstance %>Service: () => <%= entityInstance %>ServiceStub,
          <%_
            const entitiesSeen = [entityAngularName];
            for (idx in relationships) {
              if (!relationships[idx].otherEntityIsEmbedded) {
                const otherEntityName = relationships[idx].otherEntityName;
                const otherEntityNamePlural = relationships[idx].otherEntityNamePlural;
                const otherEntityAngularName = relationships[idx].otherEntityAngularName;
                if (!(otherEntityAngularName === 'User' && authenticationType === "oauth2")) {
          _%>
          <%_ if (!entitiesSeen.includes(otherEntityAngularName)) { %>
          <%= otherEntityName %>Service: () => new <%= otherEntityAngularName %>Service(),
          <%_ } _%>
          <%_ entitiesSeen.push(otherEntityAngularName); } } } _%>

        }
      });
      comp = wrapper.vm;
    });

    <%_
      let dateTestsIncluded = false;
      for (idx in fields) {
          const fieldName = fields[ idx ].fieldName;
          const fieldType = fields[idx].fieldType;
          if ([ 'Instant', 'ZonedDateTime' ].includes(fieldType) && !dateTestsIncluded) {
              dateTestsIncluded = true;
    _%>
    describe('load', () => {
      it('Should convert date from string', () => {
        // GIVEN
        const date = new Date('2019-10-15T11:42:02Z');

        // WHEN
        const convertedDate = comp.convertDateTimeFromServer(date);

        // THEN
        expect(convertedDate).toEqual(format(date, DATE_TIME_LONG_FORMAT));
      });

      it('Should not convert date if date is not present', () => {
        expect(comp.convertDateTimeFromServer(null)).toBeNull();
      });
    });
    <%_ } } _%>

    describe('save', () => {
      it('Should call update service on save for existing entity', async () => {
        // GIVEN
        const entity = {id:<%- tsKeyId %>};
        comp.<%= entityInstance %> = entity;
        <%= entityInstance %>ServiceStub.update.resolves(entity);

        // WHEN
        comp.save();
        await comp.$nextTick();

        // THEN
        expect(<%= entityInstance %>ServiceStub.update.calledWith(entity)).toBeTruthy();
        expect(comp.isSaving).toEqual(false);
      });

      it('Should call create service on save for new entity', async () => {
        // GIVEN
        const entity = {};
        comp.<%= entityInstance %> = entity;
        <%= entityInstance %>ServiceStub.create.resolves(entity);

        // WHEN
        comp.save();
        await comp.$nextTick();

        // THEN
        expect(<%= entityInstance %>ServiceStub.create.calledWith(entity)).toBeTruthy();
        expect(comp.isSaving).toEqual(false);
      });
    });

    describe('Before route enter', () => {
      it('Should retrieve data', async () => {
        // GIVEN
        const found<%= entityAngularName %> = { id: <%- tsKeyId %> };
        <%= entityInstance %>ServiceStub.find.resolves(found<%= entityAngularName %>);
        <%= entityInstance %>ServiceStub.retrieve.resolves([found<%= entityAngularName %>]);

        // WHEN
        comp.beforeRouteEnter({ params: { <%= entityInstance %>Id: <%- tsKeyId %> } }, null, cb => cb(comp));
        await comp.$nextTick();

        // THEN
        expect(comp.<%= entityInstance %>).toBe(found<%= entityAngularName %>);
      });
    });

    describe('Previous state', () => {
      it('Should go previous state', async () => {
        comp.previousState();
        await comp.$nextTick();

        expect(comp.$router.currentRoute.fullPath).toContain('/');
      });
    });
  });
});
